<template>
    <div>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="openAdd">抽样盘点</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="id"
            label="id"
            width="100">
        </el-table-column>
        <el-table-column
            prop="cropName"
            label="作物名称"
            width="100">
        </el-table-column>
        <el-table-column
            prop="cropId"
            label="作物编号"
            width="100">
        </el-table-column>
        <el-table-column
            prop="inventoryUser"
            label="盘点人"
            width="150">
        </el-table-column>
        <el-table-column
            prop="status"
            label="盘点状态"
            width="150">
          <template v-slot="s">
            <span v-if="s.row.status==0">未盘点</span>
            <span v-if="s.row.status==1">已盘点</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="title"
            label="盘点结果"
            width="150">
        </el-table-column>

        <el-table-column
            prop="cropDataSum"
            label="库存数量"
            width="150">
        </el-table-column>
        <el-table-column
            prop="cropRealitySum"
            label="爬电数量"
            width="150">
          <template v-slot="s">
            <el-input-number v-model="s.row.cropRealitySum" @change="handleChange" :min="1" label="描述文字"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span v-if="scope.row.status==0">
               <el-button
                   size="mini"
                   type="text"
                   icon="el-icon-edit"
                   @click="gopan(scope.row)"
                   v-hasPermi="['system:dict:edit']"
               >去盘点</el-button>
            </span>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="dayin(scope.row)"
                v-hasPermi="['system:dict:edit']"
            >打印盘点单</el-button>
          </template>

        </el-table-column>
      </el-table>
      <el-pagination
          @current-change="gopage"
          :page-size="size"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
      <el-dialog center title="选择盘点" :visible.sync="open" width="700px" append-to-body>
        <el-form ref="form" label-width="80px" :model="cropperForm">
          <el-row>
            <el-col :span="15">
              <el-form-item label="请选择仓库" prop="nickName">
                <el-select v-model="cropperForm.stashfId" placeholder="请选择" @change="changestashId">
                  <el-option v-for="s in fstashList" :key="s.id"   :label="s.name" :value="s.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="15">
              <el-form-item label="请选择库区" prop="nickName">
                <el-select v-model="cropperForm.stashId" placeholder="请选择" @change="changecropId">
                  <el-option v-for="s in stashList" :key="s.id"   :label="`${s.id}，${s.name} (${s.status},${s.standard}吨,尺寸:${s.size}*${s.size})`" :value="s.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="15">
              <el-form-item label="请选择盘点物品" prop="nickName">
                <el-select v-model="cropperForm.cropId" placeholder="请选择">
                  <el-option v-for="c in cropList" :key="c.id"   :label="c.cropName" :value="c.cropId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" type="primary" @click="pandian(cropperForm.cropId,cropperForm.stashId)">确 定</el-button>
          <el-button size="mini" @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

    </div>
</template>

<script>
import axios from "axios";

export default {
  name: "PanstockView.vue",
  data(){
    return{
      tableData:[],
      cropperForm:{},
      stashList:[],
      open:false,
      size:3,
      total:0,
      current:1,
      formLabelWidth:'120px',
      fstashList:[],
      cropList:[],
      fids:'',
      cropId:''
    }
  },methods:{
    dayin(row){
      let params = new URLSearchParams();
      for (let key in row) {
        if (Object.prototype.hasOwnProperty.call(row, key)) {
          params.append(key, row[key]);
        }
      }
      location.href="http://localhost:10010/cropservice/crop/dayin?id="+row.id;
    },
    gopan(row){
      console.log(row);
      this.axios.post("/cropservice/crop/gopan",row).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.msg);
          this.list();
        }
      })
    },
    openAdd(){
      this.open=true;
      this.getfstashList();

    },
    gopage(current){
      this.current=current;
      this.list();
    },
    changestashId(id){
      this.fids=id;
      this.axios.post("/cropservice/crop/changestashId?id="+id).then(res=>{
        if(res.data.code==200){
          // this.cropperForm.stashId=null;
          this.stashList=res.data.data;
        }
      })
    },
    changecropId(id){
      this.axios.post("/cropservice/crop/changecropId?stashId="+id+"&fstashId="+this.fids).then(res=>{
        if(res.data.code==200){
          this.cropList=res.data.data;
        }
      })
    },
    getfstashList(){
      this.axios.post("/cropservice/crop/getfstashList").then(res=>{
        if(res.data.code==200){
          this.fstashList=res.data.data;
        }
      })
    },

    gostorage(row){
      this.cropperForm=row;
      this.open=true;
    },
    list(){
      this.axios.post("/cropservice/crop/panlist?current="+this.current+"&size="+this.size).then(res=>{
        if(res.data.code==200){
          this.tableData=res.data.data.list;
          this.total=res.data.data.total;
        }
      })
    },
    async pandian(cropId = null,stashId=null) {
        const postData = {
          stashfId: this.cropperForm.stashfId,
          stashId: stashId,
          cropId: cropId,// 如果cropId未传递或为null，则默认为null
        };
        const response = await axios.post('/cropservice/crop/addpan', postData);
        if (response.data.code === 200) {
          this.$message.success(response.data.msg);
          this.open = false;
          this.list(); // 假设这是用来更新列表的函数
        } else {
          this.$message.error(response.data.msg);
        }
    },
  },created() {
      this.list();
  },

}
</script>

<style scoped>

</style>